package net.JwtDemo.dto;

import jakarta.persistence.Id;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import net.JwtDemo.PasswordEncoder.Md5PasswordEncoder;
import net.JwtDemo.entity.UserInfo;

import java.util.Collection;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

@Data
//@Entity
@NoArgsConstructor
@AllArgsConstructor
@Component
public class LoginUser implements UserDetails {	
	
	@Id
	private Integer id;
	//private String loginName;
	private String userName;
	private String password;	

	public LoginUser(UserInfo user) {
		this.id = user.getId();
		this.userName = user.getLoginName();
		this.password = Md5PasswordEncoder.bytesToHex(user.getPasswd());
	}
	
	public LoginUser(RequestUser user) {
		//this.loginName = user.getLoginName();
		this.userName = user.getLoginName();
		this.password = user.getPassword();
	}

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getUsername() {
		// TODO Auto-generated method stub
		return this.userName;
	}
	
	@Override
	public String getPassword() {
		return this.password;
	}
	
	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		//如果有效期存放在Redis，则这里需要查询redis，获取有效期
		//如果没有超期，则更新有效期为下个时间期，Now() + 指定有效期
		//同时返回True
		//如果超期，则返回False
		return true;
	}

	@Override
	public boolean isEnabled() {
		return true;
	}
	
}
